package com.lmz.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.lmz.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000120601267";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWWEZUk4q/8G/thFiNXarK95rAv6Pi++GbvMIWSs+sYbYOIdKv0+F7a7vn/XsbvbDtmkcbfdPzZVDKDPem9lqRygSgP4Efwrtg+kn7ge7aW3+yzvCpNxWqUhQbNBIGJ1Uh2OqG1aDTJbeqczIM1Yd+NYhpwjrQ4T9XZ626n+IWSpYn8mGCKJKAPCMe/Ojfx3jf/fLVolPjVy21DE6UnsK239IDEBsnirVG8pUxy2FgHTNhpzGGpBMGxBEftbVe8/MeFSIHhMoJCZb0+XhdhYf4b75vp5j4uwh1y2VoD+rRGPKbN2+DFPLA0nXqBAJx904GHFjT68K0OLRbyD/cYWqPAgMBAAECggEAAONGjwtL/3QpBQsKBtOtxYMOy+KV0ipQm73Sm5UXgtoU3CKA/v7OTw9EzcrmZjSJje2iS9xDWyWXA4puD/NWd5Ps0RFu7sQUnS79LtoGkg2GRIUKUMV9gEL50WtOS/EBhLCmOeObDoQjgIMroX3FTg583rsB4+fgmuCG2xpEmIMX6wy8e8yY40IKRGCUnmshww4mrC511D8WSPKkbryC74gwrm0rgODKjnhBniImF1mWJsC0xwXU2n6GN8Gg+0lpCHFjsQURGff5YJ2ANzAl/F7eNJU4B7cGaNIFKOB/6BEqJMQBs7zCR8KGQxnF1mKAinPlkBXBw60ey1T7fZ08EQKBgQDTm8c/bHTyy/rdbqbIBNbcErDg4WGw9YMHniTUrFpdnxxeP/NM8zCZRD0WL6jxF9gUbmMUrVDXb5pg1odYZqC3W+tiRjQPsuLKUZsWvGZaJo0aYUf80lg/cRgWvgPcVVGOvGIz8pK/zaztYbX4n/voXCu+sK9UU3pmS/Vb9U28CQKBgQC14mRUqnxD2gIVH5GqcpaHgpv374i7mTZcTy4BQ5dMlrAgqrxF52xYCMlIgfVO4KC80vJlywfnXVzQg36T7iS3jklpfVdDPkmhQDlzCLyAKfYa0+GFCpECAm6GTFby/Llb+oQ4bSTqgNYjkjRH5Zo0vigoXziP0+ujGJIuQeJH1wKBgHktqipd15L60Mxhg0zjyI76fSQqrSEDV2fTXlAT6yPV7A7lHFUipt/+PeUoodx+xRTeKgs7PSJCHOv7rkIncz3Ne7wpa8LZtBocJW4dsZdKoF9M+kaDxll+UEclxVw116CfrLUG4pbIF5FQ9T5Nj5p+veZTk/P8YgmW0gk5LqzRAoGBAJpcnNHOLtBXAVV0CWgi/+XZzwzI2b589Um6WUzxVELH0Iqo3Ehnc+fy7QEAanSa50/Zp6cEfJy6+75rGs3pWnf95ggFPD3xawPTQe6DB5euBRQXploG8bJG5g5MU669FYMeRYCuAJqU5SgjOQObtRf2GpbI2XQXYvB0NfvUJnrZAoGBAL9X9ogyn+qd4AczkPseJrlzyIy/bMwYDBtEG4T7d2DAMhZTj6I12L9/zJh1BIwWlw/uaU5Mx630OyhmDZgq8wXdoTXd2vN7xJ/ZY0XjHK+pKqND6YqpRKM7hncCYagCS3Cw98izSWRzH+fHMemNBhx33gagriJj1XHiCFm3jBK/";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtZmQVQ4ErC+2dq0ummdhOlWJ5jovDcZCrACucF/JPQrTICwTEShEp2ArnYLKokWOiJCDBGCVYtVHxSxSGYyFriR+l1jUzMCWx4PJaw5kOIbmKoapkIl3+LywzqSHC7JhY6wQqsJ51Kf61GGS+Prk+5aiEdk9gPxia0JkPsRZsiFWZkBW+xs1zL/X0XxCgHuJXtnc+ZzMjzwwAX3B94y7P8X5u+1Duts56DuHmwDlZUJIWjLCPOUxJWLlPRe9hCDjFBL9vWc10nTCZrHOp7X2Bxrtx/dpyqv/RRo1ZU8MOnIxQedvaFiUq3V3vbSo+MpvUC8N+nR031u9IQTyLM43XQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://53i4642b01.wicp.vip/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";
    // 签名方式
    private String sign_type = "RSA2";
    // 字符编码格式
    private String charset = "utf-8";
    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
    //订单超时时间
    private String timeout = "1m";

    public String pay(PayVo vo) throws AlipayApiException {
        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        return result;
    }
}